package avicit.bdp.dms.das.rest;

import avicit.bdp.common.service.dto.BdpTableDTO;
import avicit.bdp.core.constant.Constants;
import avicit.bdp.dms.das.dto.BdpDasCategoryDTO;
import avicit.bdp.dms.das.dto.BdpDasColumnViewSettingDTO;
import avicit.bdp.dms.das.service.BdpDasCategoryService;
import avicit.bdp.dms.das.service.BdpDasColumnViewSettingService;
import avicit.bdp.dms.das.service.BdpDasStatService;
import avicit.bdp.dms.das.service.BdpDasTableService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 数据资产管理REST接口
 */
@RestController
@Api(tags = "数据资产管理REST接口")
@RequestMapping("/api/bdp/dms/das/dataAsset")
public class BdpDasDataAssetRest {

    @Autowired
    private BdpDasCategoryService bdpDasCategoryService;

    @Autowired
    private BdpDasTableService bdpDasTableService;

    @Autowired
    private BdpDasStatService bdpDasStatService;

    @Autowired
    private BdpDasColumnViewSettingService columnViewSettingService;

    /**
     * 数据存储管理-数据查看，获取模型表 注意： 1、数据存储管理-数据查看，获取模型表，projectId实际无效，前端未传入
     */
    @ApiOperation(value = "数据存储管理-数据查看，获取模型表")
    @ApiImplicitParam(name = "projectId", value = "项目空间ID", dataType = "String")
    @GetMapping("/findCategoryAndTableList")
    public ResponseMsg<List<BdpDasCategoryDTO>> findCategoryAndTableList(
            @RequestParam(required = false) String projectId) {
        ResponseMsg<List<BdpDasCategoryDTO>> responseMsg = new ResponseMsg<>();
        List<BdpDasCategoryDTO> result = bdpDasCategoryService.findCategoryAndTableList(projectId);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 数据查看左侧树查询
     */
    @ApiOperation(value = "数据查看左侧树")
    @GetMapping("/findCategorys")
    public ResponseMsg<List<BdpDasCategoryDTO>> findCategorys() {
        ResponseMsg<List<BdpDasCategoryDTO>> responseMsg = new ResponseMsg<>();
        List<BdpDasCategoryDTO> result = bdpDasCategoryService.findCategorys();
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 数据开发工作台/数据计算，表管理时依据空间创建者过滤公共模型表
     * 注意： 1、数据开发工作台，从公共模型导入到项目时使用；
     * 2、数据计算，从公共模型导入到项目时使用；
     */
    @ApiOperation(value = "根据空间创建者获取公共模型分类及模型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目空间ID", dataType = "String"),
            @ApiImplicitParam(name = "keyWords", value = "关键字", dataType = "String"),
            @ApiImplicitParam(name = "secretLevel", value = "密级", dataType = "String")
    })
    @GetMapping("/findCategoryAndTableByProjectCreator")
    public ResponseMsg<List<BdpDasCategoryDTO>> findCategoryAndTableByProjectCreator(@RequestParam String projectId,
                                                                                     @RequestParam(required = false) String keyWords,
                                                                                     @RequestParam(required = false) String secretLevel) {
        ResponseMsg<List<BdpDasCategoryDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(bdpDasCategoryService.findCategoryAndTableByProjectCreator(projectId, keyWords, secretLevel));

        return responseMsg;
    }

    /**
     * @获取公共数据模型
     * @参数
     * @返回值
     */
    @ApiOperation(value = "物理模型获取逻辑模型分类及模型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyWords", value = "关键字", dataType = "String")
    })
    @GetMapping("/findCategoryAndTable")
    public ResponseMsg<List<BdpDasCategoryDTO>> findCategoryAndTable(
            @RequestParam(required = false) String keyWords) {
        ResponseMsg<List<BdpDasCategoryDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(
                bdpDasCategoryService.findCategoryAndTable(keyWords));

        return responseMsg;
    }

    /**
     * 按条件分页查询 注意：
     * 1、数据存储-数据查看，查询指定分类（包含自身及所有递归子节点）下模型列表；
     * 2、根据登录角色进行权限过滤(仅关注查看、数据查看权限)
     */
    @ApiOperation(value = "按条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "末级节点分类Id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "查询关键字", name = "keyWords", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/searchMiddleDataList")
    public ResponseMsg<QueryRespBean<BdpTableDTO>> searchMiddleDataList(
            @RequestParam String typeId,
            @RequestParam(required = false) String keyWords,
            @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(defaultValue = "10", required = false) Integer pageSize) {

        ResponseMsg<QueryRespBean<BdpTableDTO>> responseMsg = new ResponseMsg<>();
        QueryRespBean<BdpTableDTO> result =
                bdpDasTableService.searchMiddleDataList(typeId, keyWords, pageNo, pageSize);
        if (result.getResult() != null) {
            responseMsg.setResponseBody(result);
        } else {
            responseMsg.setResponseBody(null);
        }
        return responseMsg;
    }

    /**
     * 按条件分页查询
     */
    @ApiOperation(value = "按条件分页查询")
    @ApiImplicitParam(name = "statType", value = "统计类型", dataType = "String", paramType = "body")
    @PostMapping("/searchFirstData")
    public ResponseMsg<Map<String, Object>> searchFirstData(
            @ApiIgnore @RequestBody Map<String, Object> queryMap) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        Map<String, Object> resultMap = bdpDasStatService.searchFirstData(queryMap);
        responseMsg.setResponseBody(resultMap);
        return responseMsg;
    }

    /**
     * 分页查询表数据列表
     */
    @ApiOperation(value = "查询表数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "表模型ID", name = "tableId", dataType = "String"),
            @ApiImplicitParam(value = "参数josn", name = "paramJson", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @PostMapping(value = "/getDataListByTable")
    public ResponseMsg<Map<String, Object>> getDataListByTable(
            @ApiIgnore @RequestBody Map<String, Object> queryMap) {

        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(bdpDasStatService.getDataListByTable(queryMap));
        return responseMsg;
    }


    /**
     * 导出数据元信息
     */
    @ApiOperation(value = "导出")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "表id", name = "tableId", dataType = "String"),
            @ApiImplicitParam(value = "高级查询参数", name = "paramJson", dataType = "String")
    })
    @PostMapping("/export")
    public void export(
            @RequestBody Map<String, Object> queryMap, HttpServletResponse response) {
        bdpDasStatService.export(queryMap, response);
    }

    /**
     * 导入excel  0-不覆盖 1-覆盖
     */
    @PostMapping("/importModelExcel")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "是否覆盖", name = "cover", dataType = "Int"),
            @ApiImplicitParam(value = "导入的文件", name = "file", dataType = "MultipartFile")
    })
    @ApiOperation(value = "导入excel")
    public ResponseMsg<String> importModelExcel(Integer cover, MultipartFile file) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(bdpDasTableService.importModelExcel(cover, file));
        return responseMsg;
    }

    /**
     * 导入excel  0-不覆盖 1-覆盖
     */
    @PostMapping("/importLogicModelExcel")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "是否覆盖", name = "cover", dataType = "Int"),
            @ApiImplicitParam(value = "补充转换信息", name = "convertParams", dataType = "String"),
            @ApiImplicitParam(value = "主题id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "导入的文件", name = "file", dataType = "MultipartFile")
    })
    @ApiOperation(value = "导入excel")
    public ResponseMsg<String> importLogicModelExcel(Integer cover, String convertParams, String typeId, MultipartFile file) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(bdpDasTableService.importLogicModelExcel(cover, convertParams, typeId, file));
        return responseMsg;
    }

    /**
     * 导出数据元信息
     */
    @ApiOperation(value = "导出")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
            @ApiImplicitParam(value = "ID列表，逗号分割", name = "ids", dataType = "String")
    })
    @GetMapping("/exportModelExcel")
    public void exportModelExcel(
            @RequestParam(required = false, name = "typeId") String typeId,
            @RequestParam(required = false, name = "ids") String ids,
            HttpServletResponse response)
            throws Exception {

        List<String> idList = null;
        if (StringUtils.isNotBlank(ids)) {
            idList = Arrays.asList(ids.split(Constants.COMMA));
        }
        bdpDasTableService.exportModelExcel(typeId, idList, response);
    }

    /**
     * 下载模板
     */
    @GetMapping(value = "/downloadModelExcel")
    @ApiOperation(value = "下载表模板")
    public void downloadModelExcel(HttpServletResponse response) {
        bdpDasTableService.downloadModelExcel(response);
    }


    /**
     * 数据查看设置字段属性
     */
    @ApiOperation(value = "保存表数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "表模型ID", name = "tableId", dataType = "String"),
            @ApiImplicitParam(value = "参数josn", name = "paramJson", dataType = "String")
    })
    @PostMapping(value = "/saveColumnSetting")
    public ResponseMsg<String> saveColumnSetting(
            @ApiIgnore @RequestBody BdpDasColumnViewSettingDTO viewSettingDTO) {

        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(columnViewSettingService.saveColumnSetting(viewSettingDTO));
        return responseMsg;
    }

    @GetMapping(value = "/getColumnSetting")
    @ApiOperation(value = "查询表数据列表")
    @ApiImplicitParam(value = "表模型ID", name = "tableId", dataType = "String")
    public ResponseMsg<BdpDasColumnViewSettingDTO> getColumnSetting(@RequestParam(value = "tableId") String tableId) {

        ResponseMsg<BdpDasColumnViewSettingDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(columnViewSettingService.getColumnSetting(tableId));
        return responseMsg;
    }
}
